import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { kafkaAPI } from '@/utils/api'

export const useKafkaStore = defineStore('kafka', () => {
  // 状态
  const connectionStatus = ref({
    connected: false,
    bootstrap_servers: null,
    connected_at: null,
    error: null
  })
  
  const topics = ref([])
  const selectedTopic = ref(null)
  const loading = ref(false)
  const error = ref(null)
  
  // 连接配置
  const connectionConfig = reactive({
    bootstrap_servers: 'localhost:9092',
    security_protocol: 'PLAINTEXT',
    sasl_mechanism: 'PLAIN',
    sasl_plain_username: '',
    sasl_plain_password: ''
  })
  
  // 方法
  const setLoading = (value) => {
    loading.value = value
  }
  
  const setError = (value) => {
    error.value = value
  }
  
  const clearError = () => {
    error.value = null
  }
  
  // 测试连接
  const testConnection = async (config) => {
    setLoading(true)
    clearError()
    
    try {
      const response = await kafkaAPI.testConnection(config)
      return response
    } catch (err) {
      setError(err.message || '连接测试失败')
      throw err
    } finally {
      setLoading(false)
    }
  }
  
  // 连接到Kafka
  const connect = async (config) => {
    setLoading(true)
    clearError()
    
    try {
      const response = await kafkaAPI.connect(config)
      if (response.success) {
        connectionStatus.value = response.data
        Object.assign(connectionConfig, config)
      }
      return response
    } catch (err) {
      setError(err.message || '连接失败')
      throw err
    } finally {
      setLoading(false)
    }
  }
  
  // 断开连接
  const disconnect = async () => {
    setLoading(true)
    clearError()
    
    try {
      const response = await kafkaAPI.disconnect()
      if (response.success) {
        connectionStatus.value = {
          connected: false,
          bootstrap_servers: null,
          connected_at: null,
          error: null
        }
        topics.value = []
        selectedTopic.value = null
      }
      return response
    } catch (err) {
      setError(err.message || '断开连接失败')
      throw err
    } finally {
      setLoading(false)
    }
  }
  
  // 获取连接状态
  const getConnectionStatus = async () => {
    try {
      const response = await kafkaAPI.getConnectionStatus()
      if (response.success) {
        connectionStatus.value = response.data
      }
      return response
    } catch (err) {
      setError(err.message || '获取连接状态失败')
      throw err
    }
  }
  
  // 获取Topic列表
  const getTopics = async () => {
    setLoading(true)
    clearError()
    
    try {
      const response = await kafkaAPI.getTopics()
      if (response.success) {
        topics.value = response.data
      }
      return response
    } catch (err) {
      setError(err.message || '获取Topic列表失败')
      throw err
    } finally {
      setLoading(false)
    }
  }
  
  // 获取Topic详情
  const getTopicInfo = async (topicName) => {
    setLoading(true)
    clearError()
    
    try {
      const response = await kafkaAPI.getTopicInfo(topicName)
      return response
    } catch (err) {
      setError(err.message || '获取Topic详情失败')
      throw err
    } finally {
      setLoading(false)
    }
  }
  
  // 获取Topic分区信息
  const getTopicPartitions = async (topicName) => {
    setLoading(true)
    clearError()
    
    try {
      const response = await kafkaAPI.getTopicPartitions(topicName)
      return response
    } catch (err) {
      setError(err.message || '获取分区信息失败')
      throw err
    } finally {
      setLoading(false)
    }
  }
  
  // 发送消息
  const sendMessage = async (messageRequest) => {
    setLoading(true)
    clearError()
    
    try {
      const response = await kafkaAPI.sendMessage(messageRequest)
      return response
    } catch (err) {
      setError(err.message || '发送消息失败')
      throw err
    } finally {
      setLoading(false)
    }
  }
  
  // 批量发送消息
  const sendBatchMessages = async (batchRequest) => {
    setLoading(true)
    clearError()
    
    try {
      const response = await kafkaAPI.sendBatchMessages(batchRequest)
      return response
    } catch (err) {
      setError(err.message || '批量发送失败')
      throw err
    } finally {
      setLoading(false)
    }
  }
  
  // 选择Topic
  const selectTopic = (topic) => {
    selectedTopic.value = topic
  }
  
  // 获取计算属性
  const isConnected = () => connectionStatus.value.connected
  const getTopicByName = (name) => topics.value.find(t => t.name === name)
  
  return {
    // 状态
    connectionStatus,
    topics,
    selectedTopic,
    loading,
    error,
    connectionConfig,
    
    // 方法
    setLoading,
    setError,
    clearError,
    testConnection,
    connect,
    disconnect,
    getConnectionStatus,
    getTopics,
    getTopicInfo,
    getTopicPartitions,
    sendMessage,
    sendBatchMessages,
    selectTopic,
    
    // 计算属性
    isConnected,
    getTopicByName
  }
}) 